package com.tistory.iam1492.network;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Base64;
import android.util.Log;

import com.tistory.iam1492.beans.StarListBean;
import com.tistory.iam1492.util.ImageUtil;
import com.tistory.iam1492.util.SRVariables;

public class SRListJSONParser {
	public ArrayList<StarListBean> getSRList(String jsonResponse){
		JSONObject root = null;
		JSONArray commentJSONArray = null;
		ArrayList<StarListBean> result = new ArrayList<StarListBean>();

		try {
			root = new JSONObject(jsonResponse);
			if (root.has("starList")){
				commentJSONArray = root.optJSONArray("starList");
				
				if (commentJSONArray == null){
					commentJSONArray = new JSONArray();					
					commentJSONArray.put(root.getJSONObject("starList"));
				}
				
				for (int idx = 0; idx < commentJSONArray.length() ; idx++){
					int id;
					String name;
					int age;
					String pic;
					int vote;
					int type;
					int listingId;
					
					JSONObject commentObj = (JSONObject)commentJSONArray.get(idx);
					id = (commentObj.has("id"))?commentObj.getInt("id"):null;
					name = (commentObj.has("name"))?commentObj.getString("name"):null;
					age = (commentObj.has("age"))?commentObj.getInt("age"):null;
					pic = (commentObj.has("pic"))?commentObj.getString("pic"):null;
					vote = (commentObj.has("vote"))?commentObj.getInt("vote"):null;
					type = (commentObj.has("type"))?commentObj.getInt("type"):null;
					listingId = (commentObj.has("listingId"))?commentObj.getInt("listingId"):null;

					Log.e("JSONParser", "Name:"+name+" vote:"+vote);
					StarListBean cb = new StarListBean();
					cb.setAge(age);
					cb.setId(id);
					cb.setListingID(listingId);
					cb.setName(name);
					cb.setPic(ImageUtil.getImageBitmap(SRVariables.SR_IMG_BASE_URL + pic));
					cb.setType(type);
					cb.setVote(vote);
					result.add(cb);
				}	
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;		
	}
}
